
$(document).ready(function(){
	
	$('input[type=checkbox],input[type=radio],input[type=file]').uniform();
	
	$('select').select2();
	
	// Form Validation
    $("#basic_validate").validate({
		rules:{
			required:{
				required:true
			},
			email:{
				required:true,
				email: true
			},
			date:{
				required:true,
				date: true
			},
			url:{
				required:true,
				url: true
			}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		},
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				dataType:  'json',
				beforeSubmit:
					function(formData, jqForm, options){
						//showBlockMsg(".form", "");
						return true;
					},
				success:
					function(json, status) {
						var obj_win = json.top ? top : window;
						if(json.result == 1){
							/*
							 showBlockMsg('.form', json.msg, 'ok');
							 window.setTimeout(function() {
							 if(json.url){
							 obj_win.location.href = json.url.replace(/&amp;/g,'&').replace(/&amp;/g,'&');
							 }else{
							 obj_win.location.reload();
							 }
							 }, 2000);
							 */
							if(json.msg)
								alert(json.msg);
							if(json.url){
								obj_win.location.href = json.url.replace(/&amp;/g,'&').replace(/&amp;/g,'&');
							}else{
								obj_win.location.reload();
							}
						}else {
							if(json.msg)
								alert(json.msg);
							if(json.url){
								obj_win.location.href = json.url.replace(/&amp;/g,'&').replace(/&amp;/g,'&');
							}
							/*
							 showBlockMsg(".form", json.msg, 'error');
							 if(json.url){
							 obj_win.location.href = json.url.replace(/&amp;/g,'&').replace(/&amp;/g,'&');
							 }else{
							 window.setTimeout(function() {
							 hideBlockMsg('.form');
							 }, 2000);
							 }
							 */
						}
						//hideBlockMsg('.form');
					},
				error:
					function(xml, status){
						//hideBlockMsg('.form');
						alert(xml.responseText);
					}
			});
			return false;
		}
	});
	
	$("#number_validate").validate({
		rules:{
			min:{
				required: true,
				min:10
			},
			max:{
				required:true,
				max:24
			},
			number:{
				required:true,
				number:true
			}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
	
	$("#password_validate").validate({
		rules:{
			pwd:{
				required: true,
				minlength:6,
				maxlength:20
			},
			pwd2:{
				required:true,
				minlength:6,
				maxlength:20,
				equalTo:"#pwd"
			}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
});
